*===================================================================================*
* Ado-file: 	csxmtsst Version 1.0 
* Author: 		Chen Shao(陈志豪)
* Affiliation: 	Lanzhou University
* E-mail: 		chenzhh20@lzu.edu.cn
* License:      Mulan PSL v2
* Date: 		2025/2/17                                 
*===================================================================================*

capture program drop csxmtsst
program define csxmtsst,rclass
	version 16
	syntax varlist(min=1) [if] [in],time(varname) [st(string)]
	di in blue "晨韶学妹向您报告: "
	tsset `time'
	foreach var in `varlist' {
	    di as white "{hline}"
		local variable "`var'"
		di in yellow _n "---`variable'---"
		mat rslt=[.,.,.,.,.,.]
		matrix colnames rslt = "c( )" "t( )" "p( )" "p-vlaue" "statistics" "IS STNRY"
		
		 //DF检验
		
		qui dfuller `var',tr reg
		local p=r(p)
		if `p'>0.05{
			qui dfuller `var',reg
			local p=r(p)
			if `p'>0.05{
				qui dfuller `var', nocons reg
				local p=r(p)
				if `p'>0.05{
					di in white "--1.DF test: `variable' Non-stationary"
				}
				else{
					di in white "--1.DF test: `variable' stationary if c=t=0"
				}
			}
			else{
				mat a=r(table)
				local pcons=el(a,4,2)
				if `pcons'>0.05{
					qui dfuller `var', nocons reg
					local p=r(p)
					if `p'>0.05{
						di in white "--1.DF test: `variable' Stability unknown,Non-stationary if c=0"
					}
					else{
						di in white "--1.DF test: `variable' stationary if c=t=0"
					}
				}
				else{
					di in white "--1.DF test: `variable' stationary if c=1"
				}
			}
		}
		else{
			mat a=r(table)
			local ptren=el(a,4,2)
			if `ptren'<0.05{
				di in white "--1.DF test: `variable' Trend Stationary"
			}
			else{
				qui dfuller `var',reg
				local pcons=el(a,4,2)
				if `pcons'>0.05{
					qui dfuller `var', nocons reg
					local p=r(p)
					if `p'>0.05{
						di in white "--1.DF test: `variable' Stability unknown,Non-stationary if c=0"
					}
					else{
						di in white "--1.DF test: `variable' stationary if c=t=0"
					}
				}
				else{
					di in white "--1.DF test: `variable' stationary if c=1"
				}
			}
		}
		
		//ADF检验
		
		di in white "--2.ADF & PP & DF-GLS test (stationary = 1)"
		matrix define dfres = J(3, 6, .)
		matrix colnames dfres = "c( )" "t( )" "p( )" "p-vlaue" "statistics" "IS STNRY"
		matrix rownames dfres = "ADF test:test 1" "ADF test:test 2" "ADF test:test 3"
		forvalues mdl=1/3{
			if `mdl'==1{
				local option "tr"
				mat dfres[1,1]=1
				mat dfres[1,2]=1
			}
			if `mdl'==2{
				local option ""
				mat dfres[2,1]=1
				mat dfres[2,2]=0
			}
			if `mdl'==3{
				local option "nocons"
				mat dfres[3,1]=0
				mat dfres[3,2]=0
			}
			qui su `var'
			local i=int(12*(r(N)/100)^(1/4))
			local i=`i'-1
			while `i'>0{
				qui dfuller `var',l(`i') `option' reg
				local p=r(p)
				mat dfres[`mdl',4]=`p'
				mat dfres[`mdl',5]=r(Zt)
				if `p'<0.05{
					mat dfres[`mdl',6]=1
				}
				else {
					mat dfres[`mdl',6]=.
				}
				if `mdl'==3{
						if r(Zt)<r(cv_5){
						mat dfres[`mdl',6]=1
					}
					else {
						mat dfres[`mdl',6]=.
					}
				}
				mat a=r(table)
				local pl=el(a,4,`i'+1)
				if `pl'<0.05{
					mat dfres[`mdl',3]=`i'
					local i=-1
					}
				else {
					local i=`i'-1
				}
				if `i'==0{
					qui dfuller `var',`option'
					mat dfres[`mdl',3]=`i'
					local p=r(p)
					mat dfres[`mdl',4]=`p'
					mat dfres[`mdl',5]=r(Zt)
					if `p'<0.05{
						mat dfres[`mdl',6]=1
					}
					else {
						mat dfres[`mdl',6]=.
					}
					if `mdl'==3{
						if r(Zt)<r(cv_5){
							mat dfres[`mdl',6]=1
						}
						else {
							mat dfres[`mdl',6]=.
						}
					}
				}
			}
		}
		mat rslt=dfres
		
		 //PP检验
		
		matrix define ppres = J(3, 6, .)
		matrix colnames ppres = "c( )" "t( )" "p( )" "p-vlaue" "statistics" "IS STNRY"
		matrix rownames ppres = "PP test:test 1" "PP test:test 2" "PP test:test 3"
		forvalues mdl=1/3{
			if `mdl'==1{
				local option "tr"
				mat ppres[1,1]=1
				mat ppres[1,2]=1
			}
			if `mdl'==2{
				local option ""
				mat ppres[2,1]=1
				mat ppres[2,2]=0
			}
			if `mdl'==3{
				local option "nocons"
				mat ppres[3,1]=0
				mat ppres[3,2]=0
			}
			qui pperron `var',`option'
			local p=r(pval)
			mat ppres[`mdl',4]=`p'
			mat a = r(cvalues)
			mat ppres[`mdl',5]=el(a,2,1)
			if `p'<0.05 {
				mat ppres[`mdl',6]=1
			}
			else {
				mat ppres[`mdl',6]=.
			}
			if `mdl'==3{
				if el(a,2,1)<el(a,2,3){
					mat ppres[`mdl',6]=1
				}
				else {
					mat ppres[`mdl',6]=.
				}
			}
		}
		mat rslt=[rslt \ ppres]
		
		 //DF-GLS检验
		
		matrix define dfglsres = J(8, 6, .)
		matrix colnames dfglsres = "c( )" "t( )" "p( )" "p-vlaue" "statistics" "IS STNRY"
		matrix rownames dfglsres = "DF-GLS test:maxlag" "DF-GLS test:siclag" "DF-GLS test:maiclag" "DF-GLS test:optlag" "DF-GLS test:maxlag" "DF-GLS test:siclag" "DF-GLS test:maiclag" "DF-GLS test:optlag"
		forvalues mdl=1/2{
			if `mdl'==1{
				local option ""
				mat dfglsres[1,2]=1
				mat dfglsres[2,2]=1
				mat dfglsres[3,2]=1
				mat dfglsres[4,2]=1
				local i=1
			}
			if `mdl'==2{
				local option "not"
				mat dfglsres[5,2]=0
				mat dfglsres[6,2]=0
				mat dfglsres[7,2]=0
				mat dfglsres[8,2]=0
				local i=5
			}
			qui dfgls `var',`option'
			mat a=r(cvalues)
			foreach lags in r(maxlag) r(siclag) r(maiclag) r(optlag) {
				local tau= el(a,r(maxlag)+1-`lags',2)
				mat dfglsres[`i',5]=`tau'
				local lim= el(a,r(maxlag)+1-`lags',4)
				local lag=`lags'
				mat dfglsres[`i',3]=`lag'
				if `tau'<`lim'{
					mat dfglsres[`i',6]=1
				}
				else {
					mat dfglsres[`i',6]=.
				}
				local i=`i'+1
			}
		}
		mat rslt=[rslt \ dfglsres]
		matlist rslt,cspec(& %12s o1|o1 %8.0f & %8.0f & %8.0f & %10.3f & %10.3f o1|o1 %8.0f &) rspec(--&&-&&-&&&&&&&-)
		matrix rownames rslt ="`var':ADF test 1" "`var':ADF test 2" "`var':ADF test 3" "`var':PP test 1" "`var':PP test 2" "`var':PP test 3" "`var':DF-GLS maxlag" "`var':DF-GLS siclag" "`var':DF-GLS maiclag" "`var':DF-GLS optlag" "`var':DF-GLS maxlag" "`var':DF-GLS siclag" "`var':DF-GLS maiclag" "`var':DF-GLS optlag"
		local varnum1 : word 1 of `varlist'
		if "`varnum1'"=="`var'"{
		    mat rrslt=rslt
			matrix colnames rrslt ="Model:c( )" "Model:t( )" "Model:p( )" "Result:p-vlaue" "Result:statistics" "Judgment:Is Stnry"
		}
		else{
		    mat rrslt=[rrslt \ rslt]
		}
	}
	return matrix testresult = rrslt
	di as white "{hline}"
	di in blue _n "您布置的任务已经完成，请您过目~♥"
end